import os
import requests
import zipfile
from tqdm import tqdm

# 下载文件
def download_file(url, save_path):
    """
    从指定 URL 下载文件
    :param url: 文件下载链接
    :param save_path: 文件保存路径
    """
    if os.path.exists(save_path):
        print(f"File already exists at {save_path}. Skipping download.")
        return

    response = requests.get(url, stream=True)
    total = int(response.headers.get('content-length', 0))
    with open(save_path, 'wb') as file, tqdm(
        desc=f"Downloading {os.path.basename(save_path)}",
        total=total,
        unit='B',
        unit_scale=True,
        unit_divisor=1024,
    ) as bar:
        for data in response.iter_content(chunk_size=1024):
            size = file.write(data)
            bar.update(size)

# 解压缩文件
def extract_zip(zip_path, extract_to):
    """
    解压缩 ZIP 文件
    :param zip_path: ZIP 文件路径
    :param extract_to: 解压路径
    """
    if os.path.exists(extract_to):
        print(f"Data already extracted to {extract_to}. Skipping extraction.")
        return

    with zipfile.ZipFile(zip_path, 'r') as zip_ref:
        zip_ref.extractall(extract_to)
    print(f"Data extracted to {extract_to}.")
